package inf.coach.mobile;

import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class Neighborpoint extends Activity  {
	
    public static long userid = 0;
    public static long osm_id= 0;
    public static double lat;
    public static double lon;
    public static double lat_entrance;
    public static double lon_entrance;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nachbarpunkt);
        setTitle("funktion Seite, Nachbarpunkt Infromationen");
        
       	Bundle bundle = this.getIntent().getExtras();  
		userid = bundle.getLong("userid");
		osm_id= bundle.getLong("osmid");
		lat=bundle.getDouble("lat");
		lon=bundle.getDouble("lon");
		lat_entrance=bundle.getDouble("lat_entrance");
		lon_entrance=bundle.getDouble("lon_entrance");
		
		final boolean flag =bundle.getBoolean("flag");
        final Button direction=(Button) findViewById(R.id.button_direction);		
        final Button direction_entrance= (Button) findViewById(R.id.button_direction_door);

		final EditText distance=(EditText) findViewById(R.id.editText_distance);		
        final CheckBox level=(CheckBox)findViewById(R.id.checkBox_level);
        final CheckBox steps=(CheckBox)findViewById(R.id.checkBox_steps);        
        final CheckBox slope=(CheckBox)findViewById(R.id.checkBox_slope);
		final Button send=(Button) findViewById(R.id.button_edit);
		
		if(flag)
		{

			level.setChecked(bundle.getBoolean("level"));
			System.out.println(bundle.getBoolean("level"));
			level.setClickable(false);
			steps.setChecked(bundle.getBoolean("steps"));
			System.out.println(bundle.getBoolean("steps"));
			steps.setClickable(false);
			slope.setChecked(bundle.getBoolean("slope"));
			System.out.println(bundle.getBoolean("slope"));
			slope.setClickable(false);
			distance.setText(String.valueOf(bundle.getInt("distance")));;
			distance.setEnabled(false);
		}
		else
		{
			send.setText("Absenden");
			send.setContentDescription("Button, absenden ");
			direction.setText("GPS Informationen erhalten");
			direction.setContentDescription("GPS Informationen automatisch aufnehmen. ");
			direction_entrance.setEnabled(false);
			direction.setEnabled(false);
			
		}
		
        send.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View arg0){
        		if(flag && send.getText().equals("Bearbeiten"))
        		{
        			level.setClickable(true);
        			steps.setClickable(true);
        			slope.setClickable(true);
        			distance.setEnabled(true);
        			direction.setText("GPS Informationen erhalten");
        			direction.setContentDescription("GPS Informationen automatisch aufnehmen. ");
        			direction.setEnabled(false);
        			direction_entrance.setEnabled(false);
        			send.setText("Absenden");
        			send.setContentDescription("Button, absenden ");
        		}
        		else if(flag && send.getText().equals("Absenden"))
        		{
            		if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
    				{
    									
    				String text = webservice.updateNeiborpointAnno(osm_id,Integer.parseInt(distance.getText().toString()), level.isChecked(), steps.isChecked(), slope.isChecked());
    				System.out.println(text);
    				if (!text.equals(null) && text.equals("successful")) {
    					dialog();

    				}
    				if (text.equals(null) || text.equals("failed")) {
    					message.show(Neighborpoint.this,"Fehlgeschlagen", "Bitte überprüfen Internetverbindung!");
    				}
    				}
            			else{
        					message.show(Neighborpoint.this,"Fehler", "Keine Internetverbindung");
        				}
        		}
        		else
        		{
        		if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
				{
        			Location loc = locationinfo.getLocation(Neighborpoint.this);
        			double lat = loc.getLatitude();
        			double lon = loc.getLongitude();
        			System.out.println("NB:"+lat+"/"+lon );
									
				String text = webservice.createNeighborpointAnno(userid,osm_id,lat,lon,Integer.parseInt(distance.getText().toString()), level.isChecked(), steps.isChecked(), slope.isChecked());
				System.out.println(text);
				if (!text.equals(null) && text.equals("successful")) {
					dialog();

				}
				if (text.equals(null) || text.equals("failed")) {
					message.show(Neighborpoint.this,"Fehlgeschlagen", "Bitte überprüfen Internetverbindung!");
				}
				}
        			else{
    					message.show(Neighborpoint.this,"Fehler", "Keine Internetverbindung! ");
    				}
        		}
        	}
        	
        });
       direction.setOnClickListener(new Button.OnClickListener(){
    	   public void onClick(View arg0){
    		   
    		   if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
				{
    			   	Intent intent = new Intent();
					Bundle bundle = new Bundle();
					bundle.putDouble("lat", lat);
					bundle.putDouble("lon",lon);
					intent.setClass(Neighborpoint.this, Orientation.class);
					intent.putExtras(bundle);
					startActivity(intent);
				}
    		   else
    		   {
    			   message.show(Neighborpoint.this,"Fehler", "Keine Internetverbindung! ");
    		   }
       }
       });

		direction_entrance.setOnClickListener(new Button.OnClickListener(){
	    	   public void onClick(View arg0){
	    		   
	    		   if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
					{
	    			   	Intent intent = new Intent();
						Bundle bundle = new Bundle();
						bundle.putDouble("lat", lat_entrance);
						bundle.putDouble("lon", lon_entrance);
						intent.setClass(Neighborpoint.this, Orientation.class);
						intent.putExtras(bundle);
						startActivity(intent);
					}
	    		   else
	    		   {
	    			   message.show(Neighborpoint.this,"Fehler", "Keine Internetverbindung! ");
	    		   }
	       }
	       });
			
		}
    protected void dialog() { 
        AlertDialog.Builder builder = new Builder(Neighborpoint.this); 
        builder.setMessage("Automatisch zur letzten Seite zurück!"); 
        builder.setTitle("Erfolg"); 
        builder.setPositiveButton("ok", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        Neighborpoint.this.finish();
                    }

                }); 
        
        builder.create().show(); 
    } 
    
    protected void dialoggoback() { 
        AlertDialog.Builder builder = new Builder(Neighborpoint.this); 
        builder.setMessage("Möchten Sie zur letzten Seite zurück springen?"); 
        builder.setTitle("Information"); 
        builder.setPositiveButton("ok", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        Neighborpoint.this.finish(); 
                    }

                }); 
        builder.setNegativeButton("abbrechen", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                    } 
                }); 
        builder.create().show(); 
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
            dialoggoback(); 
            return false; 
        } 
        return false; 
    } 
    }


